package com.risk.modules.rm.service.impl;

import com.risk.modules.rm.entity.RuleList;
import com.risk.modules.rm.dao.RuleListDao;
import com.risk.modules.rm.service.RuleListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

@Service
public class RuleListServiceImpl implements RuleListService {
    private static Logger LOG = Logger.getLogger("RuleListService");

    @Autowired
    private RuleListDao ruleListDao;

    public void add(RuleList ruleList){
        ruleList.setId(UUID.randomUUID().toString().replace("-",""));
        ruleListDao.add(ruleList);
    }

    static int pageSize = 10;

    public List<RuleList> pageByCondition(int page,RuleList ruleList){

        List<RuleList> ruleLists = ruleListDao.pageByCondition((page - 1) * pageSize,pageSize,cover(ruleList));
        return ruleLists;
    }

    public RuleList get(String id){
        return ruleListDao.get(id);
    }

    public int countByCondition(RuleList ruleList){
        return ruleListDao.countByCondition(cover(ruleList));
    }

    public void del(String id,Integer userId){
        ruleListDao.del(id,userId);
    }

    public void update(RuleList ruleList){
        ruleListDao.update(ruleList);
    }


    public void updateDefault(String id,Integer userId){
        ruleListDao.updateDefault(id,userId);
        ruleListDao.setDefault(id,userId);
    }

    private String cover(RuleList ruleList){
        String whereClause = "";
        if (null != ruleList.getUserId())
            whereClause += " and user_id = '" + ruleList.getUserId() + "'";

        if(StringUtils.isBlank(ruleList.getName()) && "null".equals(ruleList.getName()))
            whereClause += " and name like '" + ruleList.getName() + "' ";

        if(StringUtils.isBlank(ruleList.getListNo()) && "null".equals(ruleList.getListNo()))
            whereClause += " and list_no like '" + ruleList.getListNo() + "' ";

        LOG.info("===============拼接的where条件为：" + whereClause);
        return whereClause;
    }
}
